package com.example.elmmerchants.Product_Management;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Sort_Page extends AppCompatActivity {

    LinearLayout set_category_layout, product_list_layout, product_set_sold, product_set_can_not_sold, edit_category, delete_product;
    CardView last_view;
    View last_left_view;

    boolean is_loading_data = false, is_loading_product_data = false, is_update_set_product_sold = false, is_set_product_can_not_sold = false, is_update_product_category = false, is_delete_product = false;
    ArrayList<String> category = new ArrayList<>();
    String email;
    List<List<String>> product_data = new ArrayList<>();
    List<byte[]> product_pic = new ArrayList<>();
    AlertDialog dialog;
    HashMap<String, Integer> choose_which_product_to_change = new HashMap<>();
    HashMap<String, String> get_product_inventory = new HashMap<>();
    int choose_how_many_category = 0;
    HashMap<String, Integer> choose_which_category = new HashMap<>();

    private class Product_Data_Thread extends Thread {

        String category;

        public Product_Data_Thread(String category) {
            this.category = category;
        }

        @Override
        public void run() {
            is_loading_product_data = false;
            product_pic.clear();
            product_data.clear();

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();

                    PreparedStatement preparedStatement2;
                    if (category.equals("全部")) {
                        String sql2 = "SELECT * FROM `" + email + "_merchant_product`;";
                        preparedStatement2 = connection.prepareStatement(sql2);
                    }
                    else {
                        String sql2 = "SELECT * FROM `" + email + "_merchant_product` WHERE category_1 = ? OR category_2 = ? OR category_3 = ? OR category_4 = ? OR category_5 = ?;";
                        preparedStatement2 = connection.prepareStatement(sql2);
                        preparedStatement2.setString(1, category);
                        preparedStatement2.setString(2, category);
                        preparedStatement2.setString(3, category);
                        preparedStatement2.setString(4, category);
                        preparedStatement2.setString(5, category);
                    }
                    ResultSet set2 = preparedStatement2.executeQuery();
                    while (set2.next()) {
                        List<String> product = new ArrayList<>();
                        product.add(String.valueOf(set2.getInt("inventory")));
                        product.add(set2.getString("name"));
                        product.add(set2.getString("category_1"));
                        product.add(set2.getString("category_2"));
                        product.add(set2.getString("category_3"));
                        product.add(set2.getString("category_4"));
                        product.add(set2.getString("category_5"));
                        product.add(String.valueOf(set2.getInt("sold")));
                        product_pic.add(set2.getBytes("picture"));
                        product.add(String.valueOf(set2.getInt("is_single_order_without_delivery")));
                        product.add(String.valueOf(set2.getDouble("product_price")));
                        product.add(String.valueOf(set2.getInt("is_auto_return_inventory")));
                        product.add(set2.getString("description"));
                        product.add(set2.getString("barcode"));
                        product.add(String.valueOf(set2.getInt("product_state")));
                        product.add(set2.getString("specification_name"));
                        product.add(set2.getString("specification_data"));
                        product.add(set2.getString("attributes_name"));
                        product.add(set2.getString("attributes_label"));
                        product.add(String.valueOf(set2.getInt("weight")));

                        product_data.add(product);
                    }
                    is_loading_product_data = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_loading_product_data) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    product_list_layout.removeAllViews();
                    for (int i = 0; i < product_data.size(); i++) {
                        choose_which_product_to_change.put(product_data.get(i).get(1), 0);
                        get_product_inventory.put(product_data.get(i).get(1), product_data.get(i).get(0));

                        View view = LayoutInflater.from(Sort_Page.this).inflate(R.layout.choose_which_product_view, null);

                        CardView cardView = view.findViewById(R.id.card_view);

                        RelativeLayout not_sold_image = view.findViewById(R.id.not_sold_image);

                        ImageView product_pic_view = view.findViewById(R.id.product_pic);
                        Bitmap decodedImage = BitmapFactory.decodeByteArray(product_pic.get(i), 0, product_pic.get(i).length);
                        product_pic_view.setImageBitmap(decodedImage);

                        TextView product_name = view.findViewById(R.id.product_name);
                        product_name.setText(product_data.get(i).get(1));

                        TextView product_inventory = view.findViewById(R.id.product_inventory);
                        product_inventory.setText(product_data.get(i).get(0));

                        TextView product_sold = view.findViewById(R.id.product_sold);
                        product_sold.setText(product_data.get(i).get(7));

                        TextView product_price = view.findViewById(R.id.product_price);
                        product_price.setText(product_data.get(i).get(9));

                        TextView dollars = view.findViewById(R.id.dollars);

                        TextView product_state = view.findViewById(R.id.product_state);

                        CheckBox choose_which_product = view.findViewById(R.id.choose_which_product);
                        int finalI = i;
                        choose_which_product.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (choose_which_product.isChecked()) {
                                    choose_which_product_to_change.replace(product_data.get(finalI).get(1), 1);
                                }
                                else {
                                    choose_which_product_to_change.replace(product_data.get(finalI).get(1), 0);
                                }
                            }
                        });

                        if (product_data.get(i).get(13).equals("1")) {
                            cardView.setBackgroundColor(Color.rgb(245, 246, 251));
                            product_name.setTextColor(Color.rgb(128, 128, 128));
                            dollars.setTextColor(Color.rgb(128, 128, 128));
                            product_price.setTextColor(Color.rgb(128, 128, 128));
                            not_sold_image.setVisibility(View.VISIBLE);
                            product_state.setText("暂停售卖");
                        }
                        else if (product_inventory.getText().toString().equals("0")) {
                            cardView.setBackgroundColor(Color.rgb(245, 246, 251));
                            product_name.setTextColor(Color.rgb(128, 128, 128));
                            dollars.setTextColor(Color.rgb(128, 128, 128));
                            product_price.setTextColor(Color.rgb(128, 128, 128));
                            not_sold_image.setVisibility(View.VISIBLE);
                            product_state.setText("暂无库存");
                        }

                        product_list_layout.addView(view);
                    }
                }
            });
        }
    }

    private class Data_Thread extends Thread {

        @Override
        public void run() {
            is_loading_data = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "SELECT * FROM merchant WHERE email = ?;";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, email);
                    ResultSet set = preparedStatement.executeQuery();
                    while (set.next()) {
                        String category_temp = "";
                        for (int i = 0; i < set.getString("shop_category").length(); i++) {
                            if (set.getString("shop_category").charAt(i) != ',') {
                                category_temp += set.getString("shop_category").charAt(i);
                            }
                            else {
                                category.add(category_temp);
                                category_temp = "";
                            }
                        }
                    }
                    is_loading_data = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_loading_data) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    set_category_layout.removeAllViews();
                    for (int i = 0; i < category.size(); i++) {
                        if (!category.get(i).equals("全部")) {
                            choose_which_category.put(category.get(i), 0);
                        }
                        View view = LayoutInflater.from(Sort_Page.this).inflate(R.layout.category_item_view, null);
                        CardView item = view.findViewById(R.id.item);
                        View left_view = view.findViewById(R.id.left_view);
                        TextView item_name = view.findViewById(R.id.item_name);
                        item_name.setText(category.get(i));
                        item_name.setPadding(0, 30, 0, 30);
                        if (i == 0) {
                            Product_Data_Thread productDataThread = new Product_Data_Thread("全部");
                            productDataThread.start();
                            item.setBackgroundColor(Color.rgb(245, 246, 251));
                            left_view.setVisibility(View.VISIBLE);
                            last_view = item;
                            last_left_view = left_view;
                        }
                        int finalI = i;
                        item.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                product_list_layout.removeAllViews();
                                product_list_layout.addView(LayoutInflater.from(Sort_Page.this).inflate(R.layout.load_data, null));
                                Product_Data_Thread productDataThread = new Product_Data_Thread(category.get(finalI));
                                productDataThread.start();
                                if (last_view != item) {
                                    item.setBackgroundColor(Color.rgb(245, 246, 251));
                                    left_view.setVisibility(View.VISIBLE);
                                    last_view.setBackgroundColor(Color.WHITE);
                                    last_left_view.setVisibility(View.GONE);
                                    last_view = item;
                                    last_left_view = left_view;
                                }
                            }
                        });
                        set_category_layout.addView(view);
                    }
                }
            });
        }
    }

    private class Set_Product_Sold_Thread extends Thread {
        @Override
        public void run() {
            is_update_set_product_sold = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();

                    for (String key : choose_which_product_to_change.keySet()) {
                        if (choose_which_product_to_change.get(key) == 1 && !get_product_inventory.get(key).equals("0")) {
                            String sql3 = "update `elm`.`" + email + "_merchant_product` set product_state = ? where name = ?";
                            PreparedStatement preparedStatement2 = connection.prepareStatement(sql3);
                            preparedStatement2.setInt(1, 0);
                            preparedStatement2.setString(2, key);
                            preparedStatement2.executeUpdate();
                        }
                    }
                    is_update_set_product_sold = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_update_set_product_sold) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    show_category();
                }
            });
        }
    }

    private class Set_Product_Can_Not_Sold_Thread extends Thread {
        @Override
        public void run() {
            is_set_product_can_not_sold = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();

                    for (String key : choose_which_product_to_change.keySet()) {
                        if (choose_which_product_to_change.get(key) == 1 && !get_product_inventory.get(key).equals("0")) {
                            String sql3 = "update `elm`.`" + email + "_merchant_product` set product_state = ? where name = ?";
                            PreparedStatement preparedStatement2 = connection.prepareStatement(sql3);
                            preparedStatement2.setInt(1, 1);
                            preparedStatement2.setString(2, key);
                            preparedStatement2.executeUpdate();
                        }
                    }
                    is_set_product_can_not_sold = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_set_product_can_not_sold) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    show_category();
                }
            });
        }
    }

    private class Update_Product_Category_Thread extends Thread {
        @Override
        public void run() {
            is_update_product_category = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();

                    for (String key : choose_which_product_to_change.keySet()) {
                        if (choose_which_product_to_change.get(key) == 1) {
                            String sql3 = "update `elm`.`" + email + "_merchant_product` set category_1 = ?, category_2 = ?, category_3 = ?, category_4 = ?, category_5 = ? where name = ?";
                            PreparedStatement preparedStatement2 = connection.prepareStatement(sql3);

                            int i = 1;
                            for (String category_key : choose_which_category.keySet()) {
                                if (choose_which_category.get(category_key) == 1) {
                                    preparedStatement2.setString(i, category_key);
                                    i += 1;
                                }
                            }
                            for (int j = i; j <= 5; j++) {
                                preparedStatement2.setString(j, "");
                            }

                            preparedStatement2.setString(6, key);
                            preparedStatement2.executeUpdate();
                        }
                    }
                    is_update_product_category = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_update_product_category) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    show_category();
                }
            });
        }
    }

    private class Delete_Product_Thread extends Thread {
        @Override
        public void run() {
            is_delete_product = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();

                    for (String key : choose_which_product_to_change.keySet()) {
                        if (choose_which_product_to_change.get(key) == 1) {
                            String sql3 = "DELETE FROM `elm`.`" + email + "_merchant_product` WHERE name = ?";
                            PreparedStatement preparedStatement2 = connection.prepareStatement(sql3);
                            preparedStatement2.setString(1, key);
                            preparedStatement2.executeUpdate();
                        }
                    }
                    is_delete_product = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_delete_product) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    show_category();
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sort_page);

        email = getIntent().getStringExtra("email");

        set_category_layout = findViewById(R.id.set_category_layout);
        product_list_layout = findViewById(R.id.product_list_layout);
        product_set_sold = findViewById(R.id.product_set_sold);
        product_set_can_not_sold = findViewById(R.id.product_set_can_not_sold);
        edit_category = findViewById(R.id.edit_category);
        delete_product = findViewById(R.id.delete_product);

        show_category();

        product_set_sold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Sort_Page.this);
                builder.setTitle("是否上架所选择的产品");
                builder.setMessage("请注意！如所选择的产品因库存不足则无法上架。");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        set_dialog();
                        Set_Product_Sold_Thread setProductSoldThread = new Set_Product_Sold_Thread();
                        setProductSoldThread.start();
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

                builder.create();
                builder.show();
            }
        });

        product_set_can_not_sold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Sort_Page.this);
                builder.setTitle("是否下架所选择的产品");
                builder.setMessage("请注意！如所选择的产品因库存不足则无法下架。");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        set_dialog();
                        Set_Product_Can_Not_Sold_Thread setProductCanNotSoldThread = new Set_Product_Can_Not_Sold_Thread();
                        setProductCanNotSoldThread.start();
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

                builder.create();
                builder.show();
            }
        });

        edit_category.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                choose_how_many_category = 0;
                Dialog dialog1 = new Dialog(view.getContext());
                dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog1.setContentView(R.layout.set_category_bottom_dialog);
                LinearLayout category_item = dialog1.findViewById(R.id.category_item);
                TextView how_many_category = dialog1.findViewById(R.id.how_many_category);
                LinearLayout ok_button = dialog1.findViewById(R.id.ok_button);
                TextView button_text = dialog1.findViewById(R.id.button_text);
                EditText search = dialog1.findViewById(R.id.search);
                ImageView close = dialog1.findViewById(R.id.close);
                close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog1.dismiss();
                    }
                });

                search.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        category_item.removeAllViews();
                        if (search.getText().length() == 0) {
                            for (String key : choose_which_category.keySet()) {
                                View view1 = LayoutInflater.from(view.getContext()).inflate(R.layout.choose_category_view, null);
                                CheckBox item = view1.findViewById(R.id.item);
                                item.setText(key);
                                if (choose_which_category.get(key) == 1) {
                                    item.setChecked(true);
                                }
                                else {
                                    item.setChecked(false);
                                }
                                item.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        if (item.isChecked() && choose_how_many_category >= 5) {
                                            item.setChecked(false);
                                            Toast.makeText(Sort_Page.this, "最多只可选择5个", Toast.LENGTH_SHORT).show();
                                        }
                                        else if (item.isChecked()) {
                                            choose_how_many_category += 1;
                                            choose_which_category.replace(key, 1);
                                        }
                                        else {
                                            choose_how_many_category -= 1;
                                            choose_which_category.replace(key, 0);
                                        }
                                        how_many_category.setText(String.valueOf(choose_how_many_category));
                                        if (choose_how_many_category >= 1) {
                                            ok_button.setBackgroundResource(R.drawable.border_yellow_5dp);
                                            button_text.setTextColor(Color.BLACK);
                                        }
                                        else if (choose_how_many_category == 0) {
                                            ok_button.setBackgroundResource(R.drawable.border_light_gray);
                                            button_text.setTextColor(Color.WHITE);
                                        }
                                    }
                                });
                                category_item.addView(view1);
                            }
                        }
                        else {
                            for (String key : choose_which_category.keySet()) {
                                if (key.indexOf(search.getText().toString()) != -1) {
                                    View view1 = LayoutInflater.from(view.getContext()).inflate(R.layout.choose_category_view, null);
                                    CheckBox item = view1.findViewById(R.id.item);
                                    item.setText(key);
                                    if (choose_which_category.get(key) == 1) {
                                        item.setChecked(true);
                                    }
                                    else {
                                        item.setChecked(false);
                                    }
                                    item.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            if (item.isChecked() && choose_how_many_category >= 5) {
                                                item.setChecked(false);
                                                Toast.makeText(Sort_Page.this, "最多只可选择5个", Toast.LENGTH_SHORT).show();
                                            }
                                            else if (item.isChecked()) {
                                                choose_how_many_category += 1;
                                                choose_which_category.replace(key, 1);
                                            }
                                            else {
                                                choose_how_many_category -= 1;
                                                choose_which_category.replace(key, 0);
                                            }
                                            how_many_category.setText(String.valueOf(choose_how_many_category));
                                            if (choose_how_many_category >= 1) {
                                                ok_button.setBackgroundResource(R.drawable.border_yellow_5dp);
                                                button_text.setTextColor(Color.BLACK);
                                            }
                                            else if (choose_how_many_category == 0) {
                                                ok_button.setBackgroundResource(R.drawable.border_light_gray);
                                                button_text.setTextColor(Color.WHITE);
                                            }
                                        }
                                    });
                                    category_item.addView(view1);
                                }
                            }
                        }
                    }
                });

                for (String key : choose_which_category.keySet()) {
                    View view1 = LayoutInflater.from(view.getContext()).inflate(R.layout.choose_category_view, null);
                    CheckBox item = view1.findViewById(R.id.item);
                    item.setText(key);
                    if (choose_which_category.get(key) == 1) {
                        item.setChecked(true);
                        choose_how_many_category += 1;
                    }
                    else {
                        item.setChecked(false);
                    }
                    item.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (item.isChecked() && choose_how_many_category >= 5) {
                                item.setChecked(false);
                                Toast.makeText(Sort_Page.this, "最多只可选择5个", Toast.LENGTH_SHORT).show();
                            }
                            else if (item.isChecked()) {
                                choose_how_many_category += 1;
                                choose_which_category.replace(key, 1);
                            }
                            else {
                                choose_how_many_category -= 1;
                                choose_which_category.replace(key, 0);
                            }
                            how_many_category.setText(String.valueOf(choose_how_many_category));
                            if (choose_how_many_category >= 1) {
                                ok_button.setBackgroundResource(R.drawable.border_yellow_5dp);
                                button_text.setTextColor(Color.BLACK);
                            }
                            else if (choose_how_many_category == 0) {
                                ok_button.setBackgroundResource(R.drawable.border_light_gray);
                                button_text.setTextColor(Color.WHITE);
                            }
                        }
                    });
                    category_item.addView(view1);
                }
                how_many_category.setText(String.valueOf(choose_how_many_category));
                if (choose_how_many_category >= 1) {
                    ok_button.setBackgroundResource(R.drawable.border_yellow_5dp);
                    button_text.setTextColor(Color.BLACK);
                }
                else if (choose_how_many_category == 0) {
                    ok_button.setBackgroundResource(R.drawable.border_light_gray);
                    button_text.setTextColor(Color.WHITE);
                }
                ok_button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (choose_how_many_category > 0) {
                            dialog1.dismiss();
                            set_dialog();
                            Update_Product_Category_Thread updateProductCategoryThread = new Update_Product_Category_Thread();
                            updateProductCategoryThread.start();
                        }
                        else {
                            Toast.makeText(Sort_Page.this, "请选择分类", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                dialog1.show();
                dialog1.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, 1500);
                dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog1.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                dialog1.getWindow().setGravity(Gravity.BOTTOM);
            }
        });

        delete_product.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Sort_Page.this);
                builder.setTitle("是否刪除所选择的产品");
                builder.setMessage("请注意！刪除后将无法恢复。");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        set_dialog();
                        Delete_Product_Thread deleteProductThread = new Delete_Product_Thread();
                        deleteProductThread.start();
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

                builder.create();
                builder.show();
            }
        });
    }

    void show_category() {
        set_category_layout.addView(LayoutInflater.from(Sort_Page.this).inflate(R.layout.load_data, null));
        product_list_layout.addView(LayoutInflater.from(Sort_Page.this).inflate(R.layout.load_data, null));
        is_loading_data = false;
        product_data.clear();
        product_pic.clear();
        category.clear();

        Data_Thread dataThread = new Data_Thread();
        dataThread.start();
    }

    public void set_dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater1 = LayoutInflater.from(this);
        builder.setView(inflater1.inflate(R.layout.loading, null));
        builder.setCancelable(false);

        dialog = builder.create();
        dialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case 1: {
                show_category();
                break;
            }
        }
    }
}